let fs = require("fs")
findDepend = (fileData) => {
    let p = /require\(*\s*\"(.*)\"\)*/g
    let p2 = /require\(*\s*\"(.*)\"\)*/i
    let arr = [];
    let matchArr = fileData.match(p);
    if (matchArr && matchArr.length) {
        for (const str of fileData.match(p)) {
            arr.push(str.match(p2)[1]);
        }
    }
    return arr;
}

dependFile = (file, luaPath, dependTable) => {
    if (!fs.existsSync(file)) return
    dependTable = dependTable || {};
    let fileData = fs.readFileSync(file, { encoding: "ascii" })
    console.log("正在打包", file)
    dependTable[luaPath || file] = fileData;
    let arr = findDepend(fileData);
    arr.forEach(v => {
        console.log(v);
        let path = process.cwd() + "\\" + v.replace(/\./g, "\\") + ".lua";
        if (!dependTable[v]) {
            dependFile(path, v, dependTable)
        }
    });
}
module.exports = { dependFile }